package com.phuc.game.tankshooter.sprite;

import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.util.MathUtils;
import org.anddev.andengine.util.constants.TimeConstants;

import com.phuc.game.tankshooter.MainActivity;

public class FuelTruckDeadSprite extends AnimatedSprite implements TimeConstants {

	public static final int WIDTH = 100;
	public static final int HEIGHT = 100;
	
	Scene scene;
	boolean isRemoved;
	float alpha;
	int timeSmoke;
	float tileW;
	float tileH;

	public FuelTruckDeadSprite(float pX, float pY, float pTileWidth, float pTileHeight,
			TiledTextureRegion pTiledTextureRegion, float rotation, Scene scene) {
		super(pX, pY, pTileWidth, pTileHeight, pTiledTextureRegion);
		tileW = pTileWidth;
		tileH = pTileHeight;
		this.scene = scene;
		alpha = 1.0f;
		isRemoved = false;
		this.setRotation(rotation);
		timeSmoke = 0;
	}

	@Override
	public void onManagedUpdate(final float pSecondsElapsed) {
		super.onManagedUpdate(pSecondsElapsed);
		
		timeSmoke++;
		if (timeSmoke % 20 == 0) {
			int xRandom = MathUtils.random(-35, 35);
			int yRandom = MathUtils.random(-35, 35);
			MainActivity.getInstant().getGamePlay().createSmoke(
					getX() + tileW / 2 + xRandom, getY() + tileH / 2 + yRandom, 1);
		}
		
		if (timeSmoke % 80 == 0) {
			int xRandom = MathUtils.random(-100, 100);
			int yRandom = MathUtils.random(-100, 100);
			MainActivity.getInstant().getGamePlay().createSmoke(
					getX() + tileW / 2 + xRandom, getY() + tileH / 2 + yRandom, 0);
		}
		
		alpha -= 0.00025f;
		this.setAlpha(alpha);
		if (alpha <= 0.5) {
			MainActivity.getInstant().runFromUpdateThread(new Runnable() {
				@Override
				public void run() {
					FuelTruckDeadSprite.this.detachSelf();
					FuelTruckDeadSprite.this.setIgnoreUpdate(true);
					FuelTruckDeadSprite.this.clearEntityModifiers();
					FuelTruckDeadSprite.this.clearUpdateHandlers();
				}
			});
		}
	}
}
